<?php


namespace IDock\Insurance\CHAC;

use Exception;
use IDock\Http\Response;
use IDock\IDock\IDockEnums;
use IDock\IDock\IDockInterface;
use IDock\Insurance\CHAC\Request\AllCHACRequest;
use IDock\Insurance\CHAC\Request\ARenewalInquiryRequest;
use IDock\Insurance\CHAC\Request\BVehicleModelRequest;
use IDock\Insurance\CHAC\Request\CVehicleActualValueRequest;
use IDock\Insurance\CHAC\Request\DPremiumInquiryRequest;
use IDock\Insurance\CHAC\Request\EUnderwritingRequest;
use IDock\Insurance\CHAC\Request\FUploadFileRequest;
use IDock\Insurance\CHAC\Request\GPolicyPaymentRequest;
use IDock\Insurance\CHAC\Request\HEPolicyDownloadRequest;
use IDock\Insurance\CHAC\Request\HNCEPolicyDownloadRequest;
use IDock\Insurance\CHAC\Request\INonCarProductListRequest;
use IDock\Insurance\CHAC\Request\J1ProposalStatusRequest;
use IDock\Insurance\CHAC\Request\J2ProposalRecallRequest;
use IDock\Insurance\CHAC\Request\K1PolicyStatusRequest;
use IDock\Insurance\CHAC\Request\K2PolicyRecallRequest;
use IDock\Insurance\CHAC\Request\ValidateCarRequest;
use IDock\Insurance\CHAC\Response\ARenewalInquiryResponse;
use IDock\Insurance\CHAC\Response\BVehicleModelResponse;
use IDock\Insurance\CHAC\Response\CVehicleActualValueResponse;
use IDock\Insurance\CHAC\Response\DPremiumInquiryResponse;
use IDock\Insurance\CHAC\Response\EUnderwritingResponse;
use IDock\Insurance\CHAC\Response\FUploadFileResponse;
use IDock\Insurance\CHAC\Response\GPolicyPaymentResponse;
use IDock\Insurance\CHAC\Response\HEPolicyDownloadResponse;
use IDock\Insurance\CHAC\Response\HNCEPolicyDownloadResponse;
use IDock\Insurance\CHAC\Response\INonCarProductListResponse;
use IDock\Insurance\CHAC\Response\J1ProposalStatusResponse;
use IDock\Insurance\CHAC\Response\J2ProposalRecallResponse;
use IDock\Insurance\CHAC\Response\K1PolicyStatusResponse;
use IDock\Insurance\CHAC\Response\K2PolicyRecallResponse;
use IDock\Insurance\CHAC\Response\ValidateCarResponse;
use IDock\Visitor\EPolicyDownloadVisitor;
use IDock\Visitor\NonCar\NonCarInquiryVisitor;
use IDock\Visitor\NonCar\NonCarProductListVisitor;
use IDock\Visitor\PolicyInfoVisitor;
use IDock\Visitor\PolicyPaymentVisitor;
use IDock\Visitor\Premium\PremiumInquiryVisitor;
use IDock\Visitor\ProposalRecallVisitor;
use IDock\Visitor\ProposalStatusVisitor;
use IDock\Visitor\RenewalInquiryVisitor;
use IDock\Visitor\UnderwritingRecallVisitor;
use IDock\Visitor\UploadFileVisitor;
use IDock\Visitor\ValidateCarVisitor;
use IDock\Visitor\VehicleActualValueVisitor;
use IDock\Visitor\VehicleModelVisitor;

class CHACConstruct extends Builder implements IDockInterface
{
    public $config = [];

    public function __construct($config)
    {
        $this->config = $config;
    }

    /**
     * 续保查询
     * @param RenewalInquiryVisitor $renewalInquiryVisitor
     * @return ARenewalInquiryResponse
     */
    public function renewalInquiry(RenewalInquiryVisitor $renewalInquiryVisitor): ARenewalInquiryResponse
    {
        $request = new ARenewalInquiryRequest();
        $request->setVisitor($renewalInquiryVisitor);

        /*** @var ARenewalInquiryResponse $response */
        $response = $this->execute($request);
        return $response;

    }

    /**
     * 车型查询
     * @param VehicleModelVisitor $vehicleModelVisitor
     * @return BVehicleModelResponse
     */
    public function vehicleModel(VehicleModelVisitor $vehicleModelVisitor): BVehicleModelResponse
    {
        $request = new BVehicleModelRequest();
        $request->setVisitor($vehicleModelVisitor);

        /*** @var BVehicleModelResponse $response */
        $response = $this->execute($request);

        return $response;
    }

    /**
     * 车辆实际价格
     * @param VehicleActualValueVisitor $vehicleActualValueVisitor
     * @return CVehicleActualValueResponse
     */
    public function vehicleActualValue(VehicleActualValueVisitor $vehicleActualValueVisitor): CVehicleActualValueResponse
    {
        $response = new CVehicleActualValueResponse(200);
        $response->setVisitor($vehicleActualValueVisitor);
        $response->printerInit();

        return $response;
    }

    /**
     * 询价前置
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     * @return void
     */
    public function preInquiry(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
    }

    public function validateCar(PremiumInquiryVisitor $premiumInquiryVisitor): Response
    {
        return new Response(200);
    }

    /**
     * 询价
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     * @return DPremiumInquiryResponse
     * @throws Exception
     */
    public function premiumInquiry(PremiumInquiryVisitor $premiumInquiryVisitor): DPremiumInquiryResponse
    {
        $request = new DPremiumInquiryRequest;
        $request->setVisitor($premiumInquiryVisitor);

        /*** @var DPremiumInquiryResponse $response */
        $response = $this->execute($request);

        if ($response->isSuccess()) {
            $response->bindUnderWritingData($request);
        }

        return $response;
    }

    /**
     * 核保
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     * @return EUnderwritingResponse
     */
    public function underwriting(PremiumInquiryVisitor $premiumInquiryVisitor): EUnderwritingResponse
    {
        $request = new EUnderwritingRequest();
        $request->setVisitor($premiumInquiryVisitor);

        /*** @var EUnderwritingResponse $response */
        $response = $this->execute($request);

        return $response;
    }

    /**
     * 文件上传
     * @param UploadFileVisitor $uploadFileVisitor
     * @return FUploadFileResponse
     * @throws Exception
     */
    public function uploadFile(UploadFileVisitor $uploadFileVisitor): FUploadFileResponse
    {
        $request = new FUploadFileRequest();
        $request->setVisitor($uploadFileVisitor);
        $request->init();

        $eMsg = [];

        foreach ($uploadFileVisitor->fileList as $value) {
            $request->file = $value;
            $uploadFileResponse = $this->execute($request);

            if (!$uploadFileResponse->isSuccess()) {
                $eMsg[] = IDockEnums::$uploadFileType[$value['type']].'-'.$uploadFileResponse->getError();
            }
        }

        if ($eMsg) {
            throw new Exception(implode(',',$eMsg));
        }

        return new FUploadFileResponse(200);
    }

    /**
     * 支付
     * @param PolicyPaymentVisitor $paymentVisitor
     * @return GPolicyPaymentResponse
     */
    public function policyPayment(PolicyPaymentVisitor $paymentVisitor): GPolicyPaymentResponse
    {
        $request = new GPolicyPaymentRequest();

        $request->loadConfig($this->config);

        $request->setVisitor($paymentVisitor);

        $response = new GPolicyPaymentResponse(200);

        $response->request = $request;

        $response->printerInit();

        return $response;
    }

    /**
     * 电子保单下载
     * @param EPolicyDownloadVisitor $EPolicyVisitor
     * @return HEPolicyDownloadResponse
     */
    public function EPolicyDownload(EPolicyDownloadVisitor $EPolicyVisitor): HEPolicyDownloadResponse
    {
        $request = new HEPolicyDownloadRequest();
        $request->setVisitor($EPolicyVisitor);

        /*** @var HEPolicyDownloadResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 非车电子保单下载
     * @param EPolicyDownloadVisitor $EPolicyVisitor
     * @return HNCEPolicyDownloadResponse
     */
    public function ENoCarPolicyDownload(EPolicyDownloadVisitor $EPolicyVisitor): HNCEPolicyDownloadResponse
    {
        $request = new HNCEPolicyDownloadRequest();
        $request->loadConfig($this->config);
        $request->setVisitor($EPolicyVisitor);

        $response = new HNCEPolicyDownloadResponse(200);

        $response->request = $request;

        $response->printerInit();

        return $response;
    }

    /**
     * 驾意险列表
     * @param NonCarProductListVisitor $nonCarProductListVisitor
     * @return INonCarProductListResponse
     */
    public function nonCarProductList(NonCarProductListVisitor $nonCarProductListVisitor): INonCarProductListResponse
    {
        $request = new INonCarProductListRequest();
        $request->setVisitor($nonCarProductListVisitor);

        /*** @var INonCarProductListResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 投保单状态查询
     * @param ProposalStatusVisitor $proposalStatusVisitor
     * @return J1ProposalStatusResponse
     */
    public function proposalStatus(ProposalStatusVisitor $proposalStatusVisitor): J1ProposalStatusResponse
    {
        $request = new J1ProposalStatusRequest();
        $request->setVisitor($proposalStatusVisitor);

        /*** @var J1ProposalStatusResponse $response */
        $response = $this->execute($request);

        return $response;
    }

    /**
     * 投保单撤销
     * @param ProposalRecallVisitor $revokeVisitor
     * @return J2ProposalRecallResponse
     */
    public function proposalRecall(ProposalRecallVisitor $revokeVisitor): J2ProposalRecallResponse
    {
        $request = new J2ProposalRecallRequest();
        $request->setVisitor($revokeVisitor);

        /*** @var J2ProposalRecallResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 保单状态
     * @param PolicyInfoVisitor $policyInfoVisitor
     * @return K1PolicyStatusResponse
     */
    public function policyInfo(PolicyInfoVisitor $policyInfoVisitor): K1PolicyStatusResponse
    {
        $request = new K1PolicyStatusRequest();
        $request->setVisitor($policyInfoVisitor);

        /*** @var K1PolicyStatusResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 人工核保撤销
     * @param UnderwritingRecallVisitor $recallVisitor
     * @return K2PolicyRecallResponse
     */
    public function underwritingRecall(UnderwritingRecallVisitor $recallVisitor): K2PolicyRecallResponse
    {
        return new K2PolicyRecallResponse(200);

        $request = new K2PolicyRecallRequest();
        $request->setVisitor($recallVisitor);

        /*** @var K2PolicyRecallResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    public function nonCarInquiry(NonCarInquiryVisitor $nonCarInquiryVisitor): Response
    {
        return new Response(200);
    }

    public function nonCarUnderwriting(NonCarInquiryVisitor $inquiryPrinter): Response
    {
        return new Response(200);
    }

    /**
     * 新车备案
     * @return void
     */
    public function newVehicleRegistration(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
    }

    /**
     * 验车码
     * @param ValidateCarVisitor $validateCarVisitor
     * @return ValidateCarResponse
     */
    public function validateCarCode(ValidateCarVisitor $validateCarVisitor): ValidateCarResponse
    {
        $request = new ValidateCarRequest();
        $request->setVisitor($validateCarVisitor);

        /*** @var $response ValidateCarResponse */
        $response = $this->execute($request);

        return $response;
    }
}
